<template>
  <el-card class="card-body">
    <el-row>
      <el-table
        :data="tableData"
        class="table-body"
        :header-cell-style="{ background: '#F4F6F9', color: '#1D2129' }"
      >
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="name" label="标题">
          <template slot-scope="scope">
            <el-link type="primary" @click="handleCheck(scope.row)">{{
              scope.row.name
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="moduleName"
          label="所属栏目"
          width="220"
        ></el-table-column>
        <el-table-column prop="updateDate" width="200" label="时间"></el-table-column>
        <el-table-column prop="userName"  width="120" label="作者"></el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" @click="handlePass(scope.row.id)"
              >通过</el-button
            >
            <el-button type="text" @click="handleReject(scope.row.id)"
              >驳回</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :count="Number(count)"
        :current="1"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
      <Check ref="check" @getParentTableData="getTableData" />
    </el-row>
  </el-card>
</template>

<script>
import Pagination from "@/components/Pagination.vue";
import { approvePass, approveReject, getSysApproveListApi } from "@/api/modules/approve.api";
import { mapMutations } from "vuex";
import Check from "@/templates/Writings/Gallery/Check.vue";

export default {
  components: {
    Pagination,
    Check
  },
  data() {
    return {
      tableData: [],
      count: 0,
      page: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  mounted() {
    this.getTableData(this.page);
  },
  methods: {
    ...mapMutations("base", ["setBreadcrumb"]),
    async getTableData({ pageNum = 1, pageSize = 10 }) {
      const res = await getSysApproveListApi({ pageNum, pageSize });
      this.tableData = res.records;
      this.count = res.total;
    },
    async handlePass(id) {
      await approvePass({id});
      await this.getTableData(this.page);
    },
    async handleReject(id) {
      await approveReject({id});
      await this.getTableData(this.page);
    },
    async handleCheck(row) {
      // 0:新闻，1：图集，2：通知公告，3：光荣榜
      if(row.bizType === 0){
        this.$router.push({
          name: "/writings/list/check",
          query: { id: row.bizId, type: "1",approveId: row.id },
        });
        this.setBreadcrumb([
          {
            name: "文章管理",
            path: "/writings",
          },
          {
            name: "文章列表",
            path: "/writings/list",
          },
          {
            name: "查看",
            path: "/writings/list/check",
          },
        ]);
      }else if(row.bizType === 2){
        this.$router.push({
          name: "/writings/notifications/check",
          query: { id: row.bizId, type: "1",approveId: row.id },
        });
        this.setBreadcrumb([
          {
            name: "文章管理",
            path: "/writings",
          },
          {
            name: "通知公告",
            path: "/writings/notifications",
          },
          {
            name: "查看",
            path: "/writings/notifications/detail",
          },
        ]);
      }else if(row.bizType === 3){
        this.$router.push({
          name: "/writings/honor/check",
          query: { id: row.bizId, type: "1",approveId: row.id },
        });
        this.setBreadcrumb([
          {
            name: "文章管理",
            path: "/writings",
          },
          {
            name: "光荣榜",
            path: "/writings/honor",
          },
          {
            name: "查看",
            path: "/writings/honor/detail",
          },
        ]);
      }else if(row.bizType === 1){
        await this.$refs.check.init(row.bizId,row.id);
      }

    },
    handleSizeChange(pageSize) {
      this.page.pageNum = pageSize;
      this.$emit("getTableData", this.page);
    },
    handleCurrentChange(pageNum) {
      this.page.pageNum = pageNum;
      this.$emit("getTableData", this.page);
    },
  },
};
</script>
